package net.ojbk.study.twobox.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.ojbk.study.twobox.beans.constraint.Constraint;
import net.ojbk.study.twobox.beans.po.Department;
import net.ojbk.study.twobox.dao.DepartmentMapper;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: twobox
 * Date: 2019/4/11
 * Time: 18:18
 * Description: No Description
 */
@Service
public class DepartmentManageService extends SupperService<Department> {

    @Autowired
    DepartmentMapper dao; // 由于supper是通过注入，只是使用引用的类型是其子类，所下面使用强转也是可以的。

    @Override
    public void addOne(Department o, BindingResult br) throws Exception {
        this.validated(br, Department.class);
        o.setIscancel((short) 0);
        dao.insert(o);
    }

    @Override
    public void deleteOne(int id) throws Exception {
        Department department = dao.selectByPrimaryKey(id);
        if (department == null)
            throw new Exception("非法表单！");
        if (dao.getCountByKey(id) > 0)
            throw new Exception(department.getDepname() + "部门下还有人,无法删除。");

        department.setIscancel((short) 1);
        dao.updateByPrimaryKey(department);
    }

    @Override
    public void updateOne(Department o, BindingResult br) throws Exception {
        this.validated(br, Department.class);
        dao.updateByPrimaryKeySelective(o);
    }

    @Override
    public PageInfo<Department> getAll(Integer currentPage) throws Exception {
        PageHelper.startPage(currentPage, pageSize);
        List<Department> dataList = dao.getAll();
        PageInfo<Department> page = new PageInfo<>(dataList);
        // 如果部队修改方法判断是否注销处理，会有起死回生的BUG
        return page;
    }
}
